{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T05:16:19.153111Z",
     "start_time": "2021-05-09T05:16:18.798924Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "pd.set_option(\"precision\", 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T05:16:19.160081Z",
     "start_time": "2021-05-09T05:16:19.154848Z"
    }
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"name\":         [\"alan\",\"beth\",\"charlie\",\"david\", \"edward\"],\n",
    "        \"age\" :         [34,    12,    43,       32,      77],\n",
    "        \"num_children\": [1,     0,     2,        1,       6],\n",
    "        \"num_pets\":     [1,     0,     1,        2,       0],\n",
    "        \"bank_balance\": [100.0, 10.0,  -10.0,    30.0,    30.0]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T05:16:19.340890Z",
     "start_time": "2021-05-09T05:16:19.161968Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>num_children</th>\n",
       "      <th>num_pets</th>\n",
       "      <th>bank_balance</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>alan</td>\n",
       "      <td>34</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>beth</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>charlie</td>\n",
       "      <td>43</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>-10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>david</td>\n",
       "      <td>32</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>edward</td>\n",
       "      <td>77</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      name  age  num_children  num_pets  bank_balance\n",
       "0     alan   34             1         1         100.0\n",
       "1     beth   12             0         0          10.0\n",
       "2  charlie   43             2         1         -10.0\n",
       "3    david   32             1         2          30.0\n",
       "4   edward   77             6         0          30.0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## elementwise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T05:16:19.420341Z",
     "start_time": "2021-05-09T05:16:19.342498Z"
    }
   },
   "outputs": [],
   "source": [
    "def even_number_background(cell_value):\n",
    "    \n",
    "    highlight = 'background-color: darkorange;'\n",
    "    default = ''\n",
    "    \n",
    "    if type(cell_value) in [float, int]:\n",
    "        if cell_value % 2 == 0:\n",
    "            return highlight\n",
    "    return default"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T05:16:19.756507Z",
     "start_time": "2021-05-09T05:16:19.427065Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "    #T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row0_col1 {\n",
       "            background-color:  darkorange;\n",
       "        }    #T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row0_col4 {\n",
       "            background-color:  darkorange;\n",
       "        }    #T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row1_col1 {\n",
       "            background-color:  darkorange;\n",
       "        }    #T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row1_col2 {\n",
       "            background-color:  darkorange;\n",
       "        }    #T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row1_col3 {\n",
       "            background-color:  darkorange;\n",
       "        }    #T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row1_col4 {\n",
       "            background-color:  darkorange;\n",
       "        }    #T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row2_col2 {\n",
       "            background-color:  darkorange;\n",
       "        }    #T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row2_col4 {\n",
       "            background-color:  darkorange;\n",
       "        }    #T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row3_col1 {\n",
       "            background-color:  darkorange;\n",
       "        }    #T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row3_col3 {\n",
       "            background-color:  darkorange;\n",
       "        }    #T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row3_col4 {\n",
       "            background-color:  darkorange;\n",
       "        }    #T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row4_col2 {\n",
       "            background-color:  darkorange;\n",
       "        }    #T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row4_col3 {\n",
       "            background-color:  darkorange;\n",
       "        }    #T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row4_col4 {\n",
       "            background-color:  darkorange;\n",
       "        }</style><table id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >name</th>        <th class=\"col_heading level0 col1\" >age</th>        <th class=\"col_heading level0 col2\" >num_children</th>        <th class=\"col_heading level0 col3\" >num_pets</th>        <th class=\"col_heading level0 col4\" >bank_balance</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57level0_row0\" class=\"row_heading level0 row0\" >0</th>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row0_col0\" class=\"data row0 col0\" >alan</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row0_col1\" class=\"data row0 col1\" >34</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row0_col2\" class=\"data row0 col2\" >1</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row0_col3\" class=\"data row0 col3\" >1</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row0_col4\" class=\"data row0 col4\" >100.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57level0_row1\" class=\"row_heading level0 row1\" >1</th>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row1_col0\" class=\"data row1 col0\" >beth</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row1_col1\" class=\"data row1 col1\" >12</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row1_col2\" class=\"data row1 col2\" >0</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row1_col3\" class=\"data row1 col3\" >0</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row1_col4\" class=\"data row1 col4\" >10.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57level0_row2\" class=\"row_heading level0 row2\" >2</th>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row2_col0\" class=\"data row2 col0\" >charlie</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row2_col1\" class=\"data row2 col1\" >43</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row2_col2\" class=\"data row2 col2\" >2</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row2_col3\" class=\"data row2 col3\" >1</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row2_col4\" class=\"data row2 col4\" >-10.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57level0_row3\" class=\"row_heading level0 row3\" >3</th>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row3_col0\" class=\"data row3 col0\" >david</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row3_col1\" class=\"data row3 col1\" >32</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row3_col2\" class=\"data row3 col2\" >1</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row3_col3\" class=\"data row3 col3\" >2</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row3_col4\" class=\"data row3 col4\" >30.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57level0_row4\" class=\"row_heading level0 row4\" >4</th>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row4_col0\" class=\"data row4 col0\" >edward</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row4_col1\" class=\"data row4 col1\" >77</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row4_col2\" class=\"data row4 col2\" >6</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row4_col3\" class=\"data row4 col3\" >0</td>\n",
       "                        <td id=\"T_b028a7a2_b085_11eb_8cb9_90cdb6a43e57row4_col4\" class=\"data row4 col4\" >30.0</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7f59185f15f8>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "styler = df.style.applymap(even_number_background)\n",
    "styler"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## rowwise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T05:16:19.773563Z",
     "start_time": "2021-05-09T05:16:19.758025Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "    #T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row2_col2 {\n",
       "            background-color:  lightcoral;\n",
       "        }    #T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row3_col3 {\n",
       "            background-color:  lightcoral;\n",
       "        }    #T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row4_col2 {\n",
       "            background-color:  lightcoral;\n",
       "        }</style><table id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >name</th>        <th class=\"col_heading level0 col1\" >age</th>        <th class=\"col_heading level0 col2\" >num_children</th>        <th class=\"col_heading level0 col3\" >num_pets</th>        <th class=\"col_heading level0 col4\" >bank_balance</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57level0_row0\" class=\"row_heading level0 row0\" >0</th>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row0_col0\" class=\"data row0 col0\" >alan</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row0_col1\" class=\"data row0 col1\" >34</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row0_col2\" class=\"data row0 col2\" >1</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row0_col3\" class=\"data row0 col3\" >1</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row0_col4\" class=\"data row0 col4\" >100.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57level0_row1\" class=\"row_heading level0 row1\" >1</th>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row1_col0\" class=\"data row1 col0\" >beth</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row1_col1\" class=\"data row1 col1\" >12</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row1_col2\" class=\"data row1 col2\" >0</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row1_col3\" class=\"data row1 col3\" >0</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row1_col4\" class=\"data row1 col4\" >10.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57level0_row2\" class=\"row_heading level0 row2\" >2</th>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row2_col0\" class=\"data row2 col0\" >charlie</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row2_col1\" class=\"data row2 col1\" >43</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row2_col2\" class=\"data row2 col2\" >2</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row2_col3\" class=\"data row2 col3\" >1</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row2_col4\" class=\"data row2 col4\" >-10.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57level0_row3\" class=\"row_heading level0 row3\" >3</th>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row3_col0\" class=\"data row3 col0\" >david</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row3_col1\" class=\"data row3 col1\" >32</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row3_col2\" class=\"data row3 col2\" >1</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row3_col3\" class=\"data row3 col3\" >2</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row3_col4\" class=\"data row3 col4\" >30.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57level0_row4\" class=\"row_heading level0 row4\" >4</th>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row4_col0\" class=\"data row4 col0\" >edward</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row4_col1\" class=\"data row4 col1\" >77</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row4_col2\" class=\"data row4 col2\" >6</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row4_col3\" class=\"data row4 col3\" >0</td>\n",
       "                        <td id=\"T_b028a7a3_b085_11eb_8cb9_90cdb6a43e57row4_col4\" class=\"data row4 col4\" >30.0</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7f5916c13400>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def more_children_or_more_pets_background(row):    \n",
    "               \n",
    "    highlight = 'background-color: lightcoral;'\n",
    "    default = ''\n",
    "    \n",
    "    # must return one string per cell in this row\n",
    "    if row['num_children'] > row['num_pets']:\n",
    "        return [highlight, default]\n",
    "    elif row['num_pets'] > row['num_children']:\n",
    "        return [default, highlight]\n",
    "    else:\n",
    "        return [default, default]\n",
    "    \n",
    "df.style.apply(more_children_or_more_pets_background, subset=['num_children', 'num_pets'], axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## columnwise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T05:16:19.925861Z",
     "start_time": "2021-05-09T05:16:19.775280Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "    #T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row3_col3 {\n",
       "            background-color:  palegreen;\n",
       "        }    #T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row4_col2 {\n",
       "            background-color:  palegreen;\n",
       "        }</style><table id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >name</th>        <th class=\"col_heading level0 col1\" >age</th>        <th class=\"col_heading level0 col2\" >num_children</th>        <th class=\"col_heading level0 col3\" >num_pets</th>        <th class=\"col_heading level0 col4\" >bank_balance</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57level0_row0\" class=\"row_heading level0 row0\" >0</th>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row0_col0\" class=\"data row0 col0\" >alan</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row0_col1\" class=\"data row0 col1\" >34</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row0_col2\" class=\"data row0 col2\" >1</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row0_col3\" class=\"data row0 col3\" >1</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row0_col4\" class=\"data row0 col4\" >100.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57level0_row1\" class=\"row_heading level0 row1\" >1</th>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row1_col0\" class=\"data row1 col0\" >beth</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row1_col1\" class=\"data row1 col1\" >12</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row1_col2\" class=\"data row1 col2\" >0</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row1_col3\" class=\"data row1 col3\" >0</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row1_col4\" class=\"data row1 col4\" >10.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57level0_row2\" class=\"row_heading level0 row2\" >2</th>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row2_col0\" class=\"data row2 col0\" >charlie</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row2_col1\" class=\"data row2 col1\" >43</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row2_col2\" class=\"data row2 col2\" >2</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row2_col3\" class=\"data row2 col3\" >1</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row2_col4\" class=\"data row2 col4\" >-10.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57level0_row3\" class=\"row_heading level0 row3\" >3</th>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row3_col0\" class=\"data row3 col0\" >david</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row3_col1\" class=\"data row3 col1\" >32</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row3_col2\" class=\"data row3 col2\" >1</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row3_col3\" class=\"data row3 col3\" >2</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row3_col4\" class=\"data row3 col4\" >30.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57level0_row4\" class=\"row_heading level0 row4\" >4</th>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row4_col0\" class=\"data row4 col0\" >edward</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row4_col1\" class=\"data row4 col1\" >77</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row4_col2\" class=\"data row4 col2\" >6</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row4_col3\" class=\"data row4 col3\" >0</td>\n",
       "                        <td id=\"T_b028a7a4_b085_11eb_8cb9_90cdb6a43e57row4_col4\" class=\"data row4 col4\" >30.0</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7f5916c0bcc0>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def maximum_value_in_column(column):    \n",
    "               \n",
    "    highlight = 'background-color: palegreen;'\n",
    "    default = ''\n",
    "\n",
    "    maximum_in_column = column.max()\n",
    "    \n",
    "    # must return one string per cell in this column\n",
    "    return [highlight if v == maximum_in_column else default for v in column]\n",
    "    \n",
    "df.style.apply(maximum_value_in_column, subset=['num_children', 'num_pets'], axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## chained calls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-09T05:16:20.045057Z",
     "start_time": "2021-05-09T05:16:19.930031Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "    #T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row1_col2 {\n",
       "            background-color:  red;\n",
       "        }    #T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row1_col3 {\n",
       "            background-color:  red;\n",
       "        }    #T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row2_col4 {\n",
       "            color:  red;\n",
       "            : ;\n",
       "        }    #T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row4_col1 {\n",
       "            font-weight:  bold;\n",
       "            : ;\n",
       "        }    #T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row4_col3 {\n",
       "            background-color:  red;\n",
       "        }</style><table id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >name</th>        <th class=\"col_heading level0 col1\" >age</th>        <th class=\"col_heading level0 col2\" >num_children</th>        <th class=\"col_heading level0 col3\" >num_pets</th>        <th class=\"col_heading level0 col4\" >bank_balance</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57level0_row0\" class=\"row_heading level0 row0\" >0</th>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row0_col0\" class=\"data row0 col0\" >alan</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row0_col1\" class=\"data row0 col1\" >34</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row0_col2\" class=\"data row0 col2\" >1</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row0_col3\" class=\"data row0 col3\" >1</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row0_col4\" class=\"data row0 col4\" >100.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57level0_row1\" class=\"row_heading level0 row1\" >1</th>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row1_col0\" class=\"data row1 col0\" >beth</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row1_col1\" class=\"data row1 col1\" >12</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row1_col2\" class=\"data row1 col2\" >0</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row1_col3\" class=\"data row1 col3\" >0</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row1_col4\" class=\"data row1 col4\" >10.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57level0_row2\" class=\"row_heading level0 row2\" >2</th>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row2_col0\" class=\"data row2 col0\" >charlie</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row2_col1\" class=\"data row2 col1\" >43</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row2_col2\" class=\"data row2 col2\" >2</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row2_col3\" class=\"data row2 col3\" >1</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row2_col4\" class=\"data row2 col4\" >-10.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57level0_row3\" class=\"row_heading level0 row3\" >3</th>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row3_col0\" class=\"data row3 col0\" >david</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row3_col1\" class=\"data row3 col1\" >32</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row3_col2\" class=\"data row3 col2\" >1</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row3_col3\" class=\"data row3 col3\" >2</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row3_col4\" class=\"data row3 col4\" >30.0</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57level0_row4\" class=\"row_heading level0 row4\" >4</th>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row4_col0\" class=\"data row4 col0\" >edward</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row4_col1\" class=\"data row4 col1\" >77</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row4_col2\" class=\"data row4 col2\" >6</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row4_col3\" class=\"data row4 col3\" >0</td>\n",
       "                        <td id=\"T_b028a7a5_b085_11eb_8cb9_90cdb6a43e57row4_col4\" class=\"data row4 col4\" >30.0</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7f5916c13d68>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# use red font for negative bank balances\n",
    "# write the highest age in bold\n",
    "# use light red background for zero values in num_children and num_pets\n",
    "\n",
    "def red_font_negatives(series):\n",
    "    highlight = 'color: red;'\n",
    "    default = ''\n",
    "    return [highlight if e < 0 else default for e in series]  \n",
    "\n",
    "def bold_max_value_in_series(series):\n",
    "    highlight = 'font-weight: bold;'\n",
    "    default = ''\n",
    "    \n",
    "    return [highlight if e == series.max() else default for e in series]  \n",
    "\n",
    "def red_background_zero_values(cell_value):\n",
    "    highlight = 'background-color: red;'\n",
    "    default = ''\n",
    "    if cell_value == 0:\n",
    "        return highlight\n",
    "    else:\n",
    "        return default     \n",
    "\n",
    "(df\n",
    " .style\n",
    " .apply(red_font_negatives, axis=0, subset=['bank_balance'])\n",
    " .apply(bold_max_value_in_series, axis=0, subset=['age'])\n",
    " .applymap(red_background_zero_values))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
